package com.pig4cloud.pigx.contract.executer.order.matcher.impl;

import com.pig4cloud.pigx.common.customize.enums.CommonDictEnum;
import com.pig4cloud.pigx.common.customize.utils.CommonUtil;
import com.pig4cloud.pigx.contract.constant.ContractMqConstants;
import com.pig4cloud.pigx.contract.enums.ContractOrderEnum;
import com.pig4cloud.pigx.contract.executer.order.matcher.OrderMatcherManager;
import com.pig4cloud.pigx.contract.mapper.ContractOrderMapper;
import com.pig4cloud.pigx.contract.mongo.entity.ContractOrderMongo;
import com.pig4cloud.pigx.contract.mongo.entity.ContractTradeMongo;
import com.pig4cloud.pigx.contract.utils.ContractUtils;
import com.pig4cloud.pigx.robot.bo.RobotDepthMapMqBO;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 默认订单撮合处理器
 *
 * @since 2022/8/17 21:26
 */
@Component
public class RobotOrderMatcher extends LimitOrderMatcher {

    /**
     * 有参构造函数
     * <p>同时给父级对象赋值
     *
     * @since 2022/8/17 20:51
     */
    public RobotOrderMatcher(ContractOrderMapper contractOrderMapper,
                             MongoTemplate mongo,
                             AmqpTemplate rabbit,
                             CommonUtil commonUtil,
                             ContractUtils contractUtils,
                             OrderMatcherManager orderMatcherManager) {
        super(contractOrderMapper, mongo, rabbit, commonUtil, contractUtils, orderMatcherManager);
    }

    /**
     * 判断是否支持
     *
     * @param taker 订单
     * @return {@link boolean}
     * @since 2022/8/17 11:20
     */
    @Override
    public boolean support(ContractOrderMongo taker) {
        return Objects.equals(CommonDictEnum.YesOrNo.YES.getValue(), taker.getIsRobot())
                && Objects.equals(ContractOrderEnum.Type.LIMIT.getValue(), taker.getType());
    }

    /**
     * 查询订单薄时，设置额外筛选条件
     *
     * @param criteria 查询条件封装
     * @param taker    订单
     * @return {@link Criteria}
     * @since 2022/8/18 15:41
     */
    @Override
    protected Criteria setExtraCriteria(Criteria criteria, ContractOrderMongo taker) {
        // 是本人的订单
        criteria.and("userId").is(taker.getUserId())
                // 是机器人的订单
                .and("isRobot").is(CommonDictEnum.YesOrNo.YES.getValue());

        // 如果是买单，寻找价格低的订单薄
        if (Objects.equals(CommonDictEnum.YesOrNo.YES.getValue(), taker.getIsBuy())) {
            criteria.and("price").lte(taker.getPrice());
        } else {
            // 如果是卖单，寻找价格高的订单薄
            criteria.and("price").gte(taker.getPrice());
        }

        return criteria;
    }

    /**
     * 发送新增成交记录mq消息
     *
     * @param taker taker
     * @param maker maker
     * @since 2022/8/30 17:32
     */
    @Override
    protected void sendTradeMq(ContractTradeMongo taker, ContractTradeMongo maker) {
        // 发送mq消息到机器人专属队列
        rabbit.convertAndSend(ContractMqConstants.EXCHANGE_FANOUT_CONTRACT_TRADE_NEW_ROBOT_K_LINE,
                null,
                taker.getId());

        // 发送深度图参数消息
        RobotDepthMapMqBO takerBO = new RobotDepthMapMqBO();
        takerBO.setSymbolName(taker.getSymbolName());
        takerBO.setSide(taker.getSide());
        takerBO.setNum(taker.getNum());
        takerBO.setPrice(taker.getPrice());
        rabbit.convertAndSend(ContractMqConstants.QUEUE_FANOUT_CONTRACT_TRADE_NEW_ROBOT_K_LINE_DEPTH_MAP,
                null,
                takerBO);

        RobotDepthMapMqBO makerBO = new RobotDepthMapMqBO();
        makerBO.setSymbolName(maker.getSymbolName());
        makerBO.setSide(maker.getSide());
        makerBO.setNum(maker.getNum());
        makerBO.setPrice(maker.getPrice());
        rabbit.convertAndSend(ContractMqConstants.QUEUE_FANOUT_CONTRACT_TRADE_NEW_ROBOT_K_LINE_DEPTH_MAP,
                null,
                makerBO);
    }
}
